/**
 * jims
 */
package com.jims.master.modules.blood.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.blood.entity.BloodComponent;
import com.jims.master.modules.blood.entity.BloodInpDetail;
import com.jims.master.modules.blood.service.BloodComponentService;
import com.jims.master.modules.blood.service.BloodImportService;
import com.jims.master.modules.sys.entity.Dict;
import com.jims.master.modules.sys.utils.DictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 血液入库Controller
 * @author fengyg
 * @version 2017-04-07
 */
@Controller
@RequestMapping(value = "${adminPath}/blood/bloodImport")
public class BloodImportController extends BaseController {

	@Autowired
	private BloodImportService bloodImportService;
    @Autowired
    private BloodComponentService bloodComponentService;
	
	@ModelAttribute
	public BloodInpDetail get(@RequestParam(required=false) String id) {
		BloodInpDetail entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bloodImportService.get(id);
		}
		if (entity == null){
			entity = new BloodInpDetail();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(BloodInpDetail bloodInpDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BloodInpDetail> page = bloodImportService.findPage(new Page<BloodInpDetail>(request, response), bloodInpDetail);
		model.addAttribute("page", page);
		return "modules/blood/bloodImportIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(BloodInpDetail bloodInpDetail, Model model) {
		model.addAttribute("bloodInpDetail", bloodInpDetail);
		return "modules/blood/bloodImportForm";
	}

    /**
     * 获取当前机构最大入库单号
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "getMaxInpNo")
    @ResponseBody
    public StringData getMaxInpNo() {
        String inpNo = bloodImportService.getMaxInpNo();
        StringData data = new StringData();
        data.setCode("success");
        data.setData(inpNo);
        return data;
    }

    /**
     * 获取血液来源
     * @return
     * @uthor fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"getBloodFromList",""})
    @ResponseBody
    public List<Dict> getBloodFromList() {
        List<Dict> newList = new ArrayList<Dict>();
        List<Dict> list = DictUtils.getDictList("BLOOD_FROM_TYPE");
        if(null != list && list.size() > 0){
            for (Dict dict : list) {
                if(dict.getLabel().equals("血库")){
                    newList.add(dict);
                }
            }
        }
        return newList;
    }

    /**
     * @return List<BloodComponent>  返回类型
     * @throws
     * @Title: autoCompleteBloodComponent
     * @Desciption: 血液成分自动补全
     * @author fengyg
     * @date 2017-04-08
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoCompleteBloodComponent", ""})
    @ResponseBody
    public List<BloodComponent> autoCompleteBloodComponent(String q, String limit) {
        if (q != null && !q.equals("")) {
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<BloodComponent> list = bloodComponentService.findautoCompleteBloodComponentList(q, limit);
        return list;
    }

    /**
     * 血液入库保存
     * @param bloodInpDetail
     * @param model
     * @return
     * @author fengyg
     */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(BloodInpDetail bloodInpDetail, Model model) throws Exception{
        StringData data = new StringData();
        int i = bloodImportService.saveInp(bloodInpDetail);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存成功");
        } else {
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
    }

    /**
     * 加载血液库存信息
     * @param bloodInpDetail
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"loadBloodStock", ""}, method = RequestMethod.GET)
    public String loadBloodStock(BloodInpDetail bloodInpDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<BloodInpDetail> list = bloodImportService.loadBloodStock(bloodInpDetail);
        model.addAttribute("list", list);
        return "modules/blood/bloodStockQuery";
    }

    /**
     * 入库单查询
     * @param bloodInpDetail
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"bloodImportSearch",""})
    public String bloodImportQuery(BloodInpDetail bloodInpDetail,HttpServletRequest request,HttpServletResponse response,Model model){
        List<BloodInpDetail> list = bloodImportService.bloodImportQuery(bloodInpDetail);
        model.addAttribute("list",list);
        return "modules/blood/bloodImportSearch";
    }

	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(BloodInpDetail bloodInpDetail) {
	    StringData data=new StringData();
		int i= bloodImportService.delete(bloodInpDetail);
        if(i>0){
            data.setCode("success");
            data.setData("删除血液入库明细记录成功");
        }else{
            data.setCode("error");
            data.setData("删除血液入库明细记录失败");
        }
		return data;
	}

}